package com.ruoyi.integral.service.impl;

import com.ruoyi.common.constant.IntegralConstants;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.integral.domain.Integral;
import com.ruoyi.integral.domain.IntegralApproval;
import com.ruoyi.integral.domain.IntegralLog;
import com.ruoyi.integral.domain.IntegralType;
import com.ruoyi.integral.mapper.IntegralApprovalMapper;
import com.ruoyi.integral.mapper.IntegralLogMapper;
import com.ruoyi.integral.mapper.IntegralMapper;
import com.ruoyi.integral.service.IIntegralApprovalService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 审批管理 服务层实现
 * 
 * @author ruoyi
 * @date 2018-10-24
 */
@Service
public class IntegralApprovalServiceImpl implements IIntegralApprovalService 
{
	@Autowired
	private IntegralApprovalMapper integralApprovalMapper;

	@Autowired
	private IntegralLogMapper integralLogMapper;

	@Autowired
	private IntegralMapper integralMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


	/**
     * 查询审批管理信息
     * 
     * @param approvalId 审批管理ID
     * @return 审批管理信息
     */
    @Override
	public IntegralApproval selectIntegralApprovalById(Integer approvalId)
	{
	    return integralApprovalMapper.selectIntegralApprovalById(approvalId);
	}
	
	/**
     * 查询审批管理列表
     * 
     * @param integralApproval 审批管理信息
     * @return 审批管理集合
     */
	@Override
	public List<IntegralApproval> selectIntegralApprovalList(IntegralApproval integralApproval)
	{
		return integralApprovalMapper.selectIntegralApprovalList(integralApproval);
	}
	
    /**
     * 新增审批管理
     * 
     * @param integralApproval 审批管理信息
     * @return 结果
     */
	@Override
	public int insertIntegralApproval(IntegralApproval integralApproval)
	{
	    return integralApprovalMapper.insertIntegralApproval(integralApproval);
	}
	
	/**
     * 修改审批管理
     * 
     * @param integralApproval 审批管理信息
     * @return 结果
     */
	@Override
	public int updateIntegralApproval(IntegralApproval integralApproval)
	{
	    return integralApprovalMapper.updateIntegralApproval(integralApproval);
	}

	/**
     * 删除审批管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteIntegralApprovalByIds(String ids)
	{
		return integralApprovalMapper.deleteIntegralApprovalByIds(Convert.toStrArray(ids));
	}

	/**
	 * 修改审批转态 添加用户积分
	 *
	 * @param approvalId 需要修改审批转态 approvalId
	 * @return 结果
	 */
	@Override
	public int successStatus(Integer approvalId,Integer status) {
		IntegralApproval integralApproval =	integralApprovalMapper.selectIntegralApprovalById(approvalId);
		int bb=1;

		if (status.equals(IntegralConstants.SQ_FAIL)){
			integralApproval.setStatus(IntegralConstants.SQ_FAIL);
			integralApproval.setSpTime(new Date());
			int b = integralApprovalMapper.updateIntegralApproval(integralApproval);
		}else if(status.equals(IntegralConstants.SQ_CX)){
			integralApproval.setStatus(IntegralConstants.SQ_CX);
			integralApproval.setSpTime(new Date());
			int b = integralApprovalMapper.updateIntegralApproval(integralApproval);
		}else {
			integralApproval.setStatus(IntegralConstants.SQ_SUCCESS);
			integralApproval.setSpTime(new Date());
			/** 更新积分审批管理状态*/
			int b = integralApprovalMapper.updateIntegralApproval(integralApproval);
			/** 添加积分日志*/
			IntegralLog log = new IntegralLog();
			log.setApprovalNum(integralApproval.getApprovalNum());
			log.setBianIntegral(integralApproval.getSqIntegral());
			log.setGetTime(integralApproval.getApprovalTime());
			log.setIntegralTitle(integralApproval.getApprovalTitle());
			log.setIntegralContent(integralApproval.getApprovalContent());
			log.setUserDept(integralApproval.getUserDept());
			log.setUserPost(integralApproval.getUserPost());
			log.setTypeId(integralApproval.getIntegralTypeId());
			log.setUserPost(integralApproval.getUserPost());
			log.setUserName(integralApproval.getUserName());
			log.setUserPhone(integralApproval.getUserPhone());
			log.setIntegralContent(integralApproval.getApprovalContent());
			log.setGetTime(integralApproval.getSpTime());
			log.setStatus(IntegralConstants.INTEGRALLOG_DEFULLT);
			int i = integralLogMapper.insertIntegralLog(log);
			/** 通过手机号和姓名去积分表里更改总积分 */
			Integral integral = integralMapper.selectUsernameAndPhone(integralApproval.getUserName(), integralApproval.getUserPhone() + "");
			if (integral.getAddIntegral() == null) {
				integral.setAddIntegral(0 + integralApproval.getSqIntegral());
			}
			else {
				integral.setAddIntegral(integral.getAddIntegral() + integralApproval.getSqIntegral());
			}
			integral.setCountIntegral(integral.getCountIntegral() + integralApproval.getSqIntegral());
		   bb = integralMapper.updateIntegral(integral);
		}
            return bb;

	}

	/**
	 * 获取积分类型
	 *
	 * @return 参数键值
	 */
	/*@Override
	public List<IntegralType> getTypeName() {
		return integralApprovalMapper.getTypeName();
	}*/

}
